'use strict';
// 申请开通视图模块
import {
    titileInputInit
} from '../components/titleInput.js';
import {
    initVerfyBtn
} from '../components/verifyBtn.js';

var $qyName, $licence_no, $frName, $frId, $jsName, $jsNum, $fistSubmit, $lodinPassword, $passwordAgain, $nextSubmit;

// 视图初始化
function viewInit(role) {
    initViewValue();
    titileInputInit();
    initVerfyBtn();
    showFrom(role);
    bindViewEvent();
}



// 显示经理表单还是商户表单
function showFrom(role) {
    if (role == "managers") {
        $("#managers-form").show();
    } else {
        $("#merchant-form").show();
    }
}

// 验证输入框
function checkFristFromInput() {
    var fival = false;
    var isQyname = checkQYName();
    var isyyno = checklicenceNo();
    var isfrname = checkFRName();
    var isfrId = _isSFZNo();
    var isjsName = checkJSName();
    var isjsNum = _isXYKNum();
    if (isQyname && isyyno && isfrname && isfrId && isjsNum && isjsName) {
        fival = true;
    }

    return fival;
}

// 验证企业名
function checkQYName() {
    var cqyname = true;
    if ($qyName.val() == "") {
        cqyname = false;
    }
    return cqyname;
}



// 验证营业执照号
function checklicenceNo() {
    var ckval = true;
    var lnoval = $licence_no.val();

    lnoval = lnoval.replace(/\W/g, "");
    $licence_no.val(lnoval);
    if (lnoval == "") {
        ckval = false;
    }
    return ckval
}

// 验证法人名
function checkFRName() {
    var ckval = true;
    if ($frName.val() == "") {
        ckval = false;
    }
    return ckval
}

// 验证结算账户名
function checkJSName() {
    var ckval = true;
    if ($jsName.val() == "") {
        ckval = false;
    }
    return ckval
}


// 验证身份证格式
function _isSFZNo() {
    var card = $frId.val();
    card = card.replace(/[^\w\*]/g, "");
    card = card.replace("*", "X");
    card = card.replace("x", "X");
    if (card.length > 18) {
        card = card.substr(0, 18);
    }
    $frId.val(card);
    var iscarNum = true;
    // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X  
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (reg.test(card) === false) { //身份证输入不合法
        iscarNum = false;
    }

    return iscarNum;
}

// 验证信用卡格式
function _isXYKNum() {
    var sText = $jsNum.val();
    if (sText.length > 19) {
        sText = sText.substr(0, 19);
        $jsNum.val(sText);
    }
    if (sText.length >= 13 && sText.length <= 19) {
        var numStr2 = sText.match(/\d*/i)[0];
        return numStr2 == sText ? true : false;
    } else {
        return false;
    }
}

// 验证两个密码是不是空的
function checkPasswordemty() {
    var thisVal = $(this).val();
    thisVal = thisVal.replace(/[^0-9]/g, "");
    $(this).val(thisVal);
    var cpev = false;
    var cfpval = checkFristPassword();
    var cfpval2 = checkAgainPassword();
    if (cfpval && cfpval2) {
        $nextSubmit.attr("class", "weui-btn weui-btn_primary");
    } else {
        $nextSubmit.attr("class", "weui-btn weui-btn_disabled weui-btn_default");
    }

    return cpev;
}

// 验证第一个密码
function checkFristPassword() {
    var cfpval = true;
    var fristPassword = $lodinPassword.val();
    if (fristPassword == "") {
        cfpval = false;
    }

    return cfpval;
}

// 验证第二个密码
function checkAgainPassword() {
    var cfpval = false;
    var fristPassword2 = $passwordAgain.val();
    var fristPassword = $lodinPassword.val();
    if (fristPassword2 != "" && fristPassword2.length == fristPassword.length) {
        cfpval = true;
    }

    return cfpval;
}

// 第一步输入事件
function firstInputEvent() {
    var checkVal = checkFristFromInput();
    if (checkVal) {
        $fistSubmit.attr("class", "weui-btn weui-btn_primary");
    } else {
        $fistSubmit.attr("class", "weui-btn weui-btn_disabled weui-btn_default");
    }
}

// 绑定视图的一些事件
function bindViewEvent() {
    $("#merchant-form input").on("input", firstInputEvent)

    $lodinPassword.on("input", checkPasswordemty);
    $passwordAgain.on("input", checkPasswordemty);
    $("#fanhui").on("touchstart", function() {
        location.href = LoginInit_URL;
    });
}

// 初始化变量
function initViewValue() {
    $qyName = $("#qyName");
    $licence_no = $("#licence_no");
    $frName = $("#frName");
    $frId = $("#frId");
    $jsName = $("#jsName");
    $jsNum = $("#jsNum");
    $fistSubmit = $("#frist-submit");
    $lodinPassword = $("#lodin-password");
    $passwordAgain = $("#password-again");
    $nextSubmit = $("#next-submit");
}

export {
    viewInit
}